package com.hyy.taobao.controller;

import com.hyy.taobao.api.IMyCartService;
import com.hyy.taobao.api.entity.MyCart;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.MyCartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

@WebServlet("/deleteGoods")
public class MyCartDeleteGoodsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;utf-8");
        HttpSession session = req.getSession();
        User myUser = (User) session.getAttribute("myUser");
        String userName = myUser.getUserName();
        String goodsName = req.getParameter("goodsName");
        MyCart myCart = new MyCart(userName, goodsName);
        String judge = req.getParameter("judgement");
        int judgement = Integer.parseInt(judge);
        IMyCartService iMyCartService = new MyCartServiceImpl();
        switch (judgement) {
            case -1:
                boolean sb1 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb1) {

                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败！");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 0:
                boolean sb2 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb2) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败！");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 1:
                boolean sb3 = iMyCartService.myCartDeleteGoods(myCart, judgement);
                if (sb3) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "操作失败！");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            case 10:
                MyCart myCart0 = new MyCart(userName);
                boolean sb4 = iMyCartService.myCartDeleteGoods(myCart0, judgement);
                if (sb4) {
                    MyCart myCart1 = new MyCart(userName);
                    IMyCartService iMyCartService1 = new MyCartServiceImpl();
                    Set<MyCart> myCartSet = iMyCartService1.myCartSelectGoods(myCart1);
                    req.setAttribute("myCartSet", myCartSet);
                    req.getRequestDispatcher("myCart.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", "清空失败！");
                    req.getRequestDispatcher("goodsResult.jsp").forward(req, resp);
                }
                break;
            default:
                System.out.println("sb");
        }
    }
}
